package com.iepacj.concurrency.ch4.one;

import java.util.concurrent.TimeUnit;

/**
 * Created by iEpac on 16/4/23.
 */
public class ThreadState {


    public static void main(String[] args) {

        new Thread(new TimeWaiting(), "TimeWaitingThread").start();

        new Thread(new Waiting(), "WaitingThread").start();

        new Thread(new Blocked(), "BlockedThread-1").start();

        new Thread(new Blocked(), "BlockedThread-2").start();
    }


    static class TimeWaiting implements Runnable {

        @Override
        public void run() {
            while (true) {
                System.out.println(Thread.currentThread().getName());
                SleepUtils.second(100);
            }
        }
    }

    static class Waiting implements Runnable{

        @Override
        public void run() {
            while (true) {
                synchronized (Waiting.class) {
                    try {
                    System.out.println(Thread.currentThread().getName());
                        Waiting.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    static class Blocked implements Runnable {

        @Override
        public void run() {
            synchronized (Blocked.class) {
                while (true) {
                    System.out.println(Thread.currentThread().getName());
                    SleepUtils.second(100);
                }
            }
        }
    }


}
